package com.cyjt.operation.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.BaseStationListAdapter;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.BaseActivity;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.BaseStationInfo;
import com.cyjt.operation.fragment.DialogFragmentForDeviceStatusDetails;
import com.cyjt.operation.view.ActionBarLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 基站状态信息列表的Activity
 * 
 * @author LTP
 * 
 */
public class BaseStationStatusListActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	/** 切换正异常基站列表的Button */
	private Button button_switch_base_station_status_list;
	/** 基站状态的listView列表 */
	private ListView listView_base_station_status_list;
	/** 提示信息(当基站列表为空时提示) */
	private TextView textView_base_station_info;
	/** 下拉刷新控件 */
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private List<BaseStationInfo> baseStationList;
	private List<BaseStationInfo> abnormalBaseStationList = new ArrayList<BaseStationInfo>();
	private List<BaseStationInfo> normalBaseStationList = new ArrayList<BaseStationInfo>();
	private BaseStationListAdapter mBaseStationListAdapter;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_BASESTATION_STATUS_LIST_SUCCEED:
				ActionBarLayout.isShowProgressBar(false);// 访问网络成功时进度条消失
				if (!(baseStationList == null)) {
					baseStationList.clear();
				}
				normalBaseStationList.clear();
				abnormalBaseStationList.clear();
				baseStationList = (List<BaseStationInfo>) msg.obj;
				separateBaseStationListData();// 分开基站列表中正异常状态的数据
				if ("查看正常基站".equals(button_switch_base_station_status_list.getText())) {
					loadAbnomalBaseStationListData();// 加载异常的站列表数据
				} else {
					loadNormalBaseStationListData();// 加载正常的基站列表数据
				}
				break;
			case HandlerMessageCodes.FETCH_BASESTATION_STATUS_LIST_FAILED:
				ActionBarLayout.isShowProgressBar(false);// 访问网络成功时进度条消失
				Toast.makeText(BaseStationStatusListActivity.this, "从服务器获取到的基站列表信息为空", Toast.LENGTH_LONG).show();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_basestation_status_list);
		initView();
		fetchBaseStationStatusInfo();// 查询基站状态信息
	}

	/**
	 * 访问服务器来获取基站状态信息
	 */
	private void fetchBaseStationStatusInfo() {
		// TODO Auto-generated method stub
		AppContext.getInstance().getVolleyTools().fetchBaseStationStatusInfo(handler,
				AppContext.getInstance().getUser());
	}

	/**
	 * 初始化控件方法
	 */
	private void initView() {
		ActionBarLayout.setTitle("基站列表");
		ActionBarLayout.isShowProgressBar(true);
		textView_base_station_info = (TextView) findViewById(R.id.textView_base_station_info);
		listView_base_station_status_list = (ListView) findViewById(R.id.listView_base_station_status_list);
		listView_base_station_status_list.setOnItemClickListener(this);
		button_switch_base_station_status_list = (Button) findViewById(R.id.button_switch_base_station_status_list);
		button_switch_base_station_status_list.setOnClickListener(this);
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiprrefreshLayout_for_basestation_status_list);
		mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_dark, android.R.color.holo_green_dark,
				android.R.color.holo_blue_dark, android.R.color.holo_purple);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO 下拉刷新回调函数
				ActionBarLayout.isShowProgressBar(true);
				fetchBaseStationStatusInfo();
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	/**
	 * 分开基站列表中正异常状态的数据
	 */
	private void separateBaseStationListData() {
		// 把基站按照基站编码大小进行排序
		Collections.sort(baseStationList);
		for (BaseStationInfo baseStationInfo : baseStationList) {
			if ("异常".equals(baseStationInfo.getStatusToString())) {
				abnormalBaseStationList.add(baseStationInfo);
			} else {
				normalBaseStationList.add(baseStationInfo);
			}
		}
	}

	/**
	 * 加载非正常的路由列表数据
	 */
	private void loadAbnomalBaseStationListData() {
		textView_base_station_info.setText("");
		if (abnormalBaseStationList.isEmpty()) {
			textView_base_station_info.setText("异常基站列表为空");
		}
		mBaseStationListAdapter = new BaseStationListAdapter(this, abnormalBaseStationList);
		listView_base_station_status_list.setAdapter(mBaseStationListAdapter);
	}

	/**
	 * 加载正常的路由列表数据
	 */
	private void loadNormalBaseStationListData() {
		textView_base_station_info.setText("");
		if (normalBaseStationList.isEmpty()) {
			textView_base_station_info.setText("正常基站列表为空");
		}
		mBaseStationListAdapter = new BaseStationListAdapter(this, normalBaseStationList);
		listView_base_station_status_list.setAdapter(mBaseStationListAdapter);
	}

	@Override
	public void onClick(View v) {
		// TODO 控件的点击事件
		switch (v.getId()) {
		case R.id.button_switch_base_station_status_list:
			if ("查看正常基站".equals(button_switch_base_station_status_list.getText())) {
				loadNormalBaseStationListData();
				button_switch_base_station_status_list.setText("查看异常基站");
			} else {
				loadAbnomalBaseStationListData();
				button_switch_base_station_status_list.setText("查看正常基站");
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO ListView的点击事件
		BaseStationInfo baseStationInfo;
		if ("查看正常基站".equals(button_switch_base_station_status_list.getText())) {
			baseStationInfo = abnormalBaseStationList.get(position);
		} else {
			baseStationInfo = normalBaseStationList.get(position);
		}
		String baseStationStatusDetails = "基站编号：" + baseStationInfo.getCode() + "\n\n基站描述："
				+ baseStationInfo.getDescription() + "\n\n最新心跳：" + baseStationInfo.getLastAtString() + "\n\n基站类型："
				+ baseStationInfo.getTypeToString() + "\n\n基站状态：" + baseStationInfo.getStatusToString();
		DialogFragmentForDeviceStatusDetails mDeviceStatusDetails = new DialogFragmentForDeviceStatusDetails();
		Bundle args = new Bundle();
		// args.putString("title", "基站详情");
		args.putString("text", baseStationStatusDetails);
		mDeviceStatusDetails.setArguments(args);
		mDeviceStatusDetails.setStyle(DialogFragmentForDeviceStatusDetails.STYLE_NO_TITLE,
				android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth);
		mDeviceStatusDetails.show(getFragmentManager(), "baseStationStatusDetails");
	}
}
